from py2neo import Graph, Node, Relationship

# 连接到Neo4j数据库
# graph = Graph("http://localhost:7474/",auth=("neo4j", "123456"))
graph = Graph("http://192.168.86.128:7474/", user="neo4j", password="12345678", name="neo4j")

# 导出节点和关系
def export_nodes_and_relationships():
    all_data = []
    # 查询所有节点和关系
    query = "MATCH (n)-[r]->(m) RETURN n, r, m"
    result = graph.run(query)
    for record in result:
        start_node = record["n"]
        relationship = record["r"]
        end_node = record["m"]
        # 获取节点属性
        start_node_properties = dict(start_node.items())
        end_node_properties = dict(end_node.items())
        # 获取关系属性
        relationship_properties = dict(relationship.items())
        # 构建节点字符串
        start_node_str = f"({start_node.labels}, {start_node_properties})"
        end_node_str = f"({end_node.labels}, {end_node_properties})"
        # 构建关系字符串
        relationship_properties_str = ", ".join([f"{k}: {v}" for k, v in relationship_properties.items()])
        relationship_str = f"-[{relationship_properties_str}]->"     
        data = f"{start_node_str}{relationship_str}{end_node_str}"
        all_data.append(data)
    return ";".join(all_data)

merged_data = export_nodes_and_relationships()

merged_data = merged_data.replace(" ", "")

# 输出合并的数据
print(merged_data)


# 查询所有节点和关系
# result = graph.run("MATCH (n)-[r]->(m) RETURN n, r, m")
# print(result)
# 构建导出列表的函数
# def export_to_list(result):
#     export_list = []
#     for record in result:
#         node1 = record['n']
#         relationship = record['r']
#         node2 = record['m']
#         export_list.append((node1, relationship, node2))
#     return export_list

# # 导出为列表
# exported_data = export_to_list(result)

# # 打印导出结果
# for item in exported_data:
#     print(item)